package com.niuwa.p2p.action;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.niuwa.p2p.core.json.DateJsonValueProcessor;
import com.niuwa.p2p.core.json.InvisibleFilter;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.Pagination;
import com.niuwa.p2p.core.utils.JsonUtil;
import com.niuwa.p2p.core.utils.WebUtils;

/**
 * @ClassName: JsonAction
 * @Description: JSON格式输出基础类
 * @author: zhangbo
 * @date:2015年1月12日 下午2:32:14
 */
public class JsonAction extends BaseAction implements ServletRequestAware,
		ServletResponseAware {

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	/**
	 * @Fields: serialVersionUID
	 */
	private static final long serialVersionUID = 5459150260661261429L;

	private HttpServletRequest request;

	private HttpServletResponse response;

	private JsonResult result;

	protected static final JsonResult RESULT_NO_REALIZED = new JsonResult(
			false, "功能暂未实现.");

	private static final String DEFAULT_SUCCESS_MESSAGE = "操作成功.";
	protected static final JsonResult RESULT_SUCCESS = new JsonResult(true,
			DEFAULT_SUCCESS_MESSAGE);

	private static final String DEFAULT_FAILED_MESSAGE = "操作失败.";
	protected static final JsonResult RESULT_FAILED = new JsonResult(false,
			DEFAULT_FAILED_MESSAGE);

	protected static final String datePattern = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 将数据写成Json串
	 * 
	 * @Title: writeToJSON
	 * @param @param data
	 * @throws
	 */
	public void writeToJSON(Object data) {
		try {
			response.setContentType(WebUtils.MediaTypes.TEXT_HTML_UTF_8);
			JsonConfig config = new JsonConfig();
			config.setJsonPropertyFilter(new InvisibleFilter());
			config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
			config.registerJsonValueProcessor(Date.class,
					new DateJsonValueProcessor(datePattern));
			config.registerJsonValueProcessor(java.sql.Date.class,
					new DateJsonValueProcessor(datePattern));
			config.registerJsonValueProcessor(java.sql.Timestamp.class,
					new DateJsonValueProcessor(datePattern));

			// 重写Pagination对象
			JsonResult jsonResult = (JsonResult) data;
			Object dataObj = jsonResult.getData();
			if (null != dataObj && dataObj instanceof Pagination<?>) {
				response.getWriter().write(
						JSONSerializer.toJSON(jsonResult.getData(), config)
								.toString());
			} else {
				response.getWriter().write(
						JSONSerializer.toJSON(data, config).toString());
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	@Override
	@Action("save")
	public String save() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	@Action("update")
	public String update() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	@Action("list")
	public String list() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	@Action("detail")
	public String detail() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	@Action("tree")
	public String tree() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	@Action("delete")
	public String delete() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	public String importFile() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	public String exportFile() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	public String print() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	public String download() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	public String lock() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	public String active() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	public String audit() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	public String review() {
		writeToJSON(RESULT_NO_REALIZED);
		return NONE;
	}

	@Override
	public String add() {
		return ADD_OR_EDIT;
	}

	@Override
	public String edit() {
		return ADD_OR_EDIT;
	}

	public JsonResult getResult() {
		return result;
	}

	public void setResult(JsonResult result) {
		this.result = result;
	}

	protected void setDefaultSuccessResult() {
		setResult(RESULT_SUCCESS);
	}

	protected void setDefaultFailedResult() {
		setResult(RESULT_FAILED);
	}

	protected void setSuccessResult(String message) {
		setSuccessResult(message, null);
	}

	protected void setSuccessResult(String message, Object data) {
		setResult(true, message == null ? DEFAULT_SUCCESS_MESSAGE : message,
				data);
	}

	protected void setResult(boolean flag, String message, Object data) {
		setResult(new JsonResult(flag, message, data));
	}

	/**
	 * @Title: setNodeReSult
	 * @Description: 此方法为树状结构数据专用
	 * @param data
	 */
	public void setObjectReSult(Object data) {
		try {
			response.setContentType(WebUtils.MediaTypes.JSON_UTF_8);
			response.getWriter().write(JsonUtil.toJSONStringWithSerializerFeature(data));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
